package at.fhv.components.erode;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Customizer;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.media.jai.KernelJAI;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import at.fhv.framework.customizerTools.MatrixTablePanel;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * The DilateCustomizer class.
 * 
 * @author Michael Sieber
 */
public class ErodeCustomizer extends JPanel implements Customizer, ActionListener 
{
	private static final long	serialVersionUID	= 8933956613785797277L;
	
	private PropertyChangeSupport _pcsupport = new PropertyChangeSupport(this);
	
	private JButton _apply;
	private ErodeFilter _erodeFilter;
	private MatrixTablePanel _matrixTablePanel;

	public ErodeCustomizer() 
	{
		initGUI();
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void setObject(Object obj) 
	{
		_erodeFilter = (ErodeFilter) obj;
		KernelJAI kernel = _erodeFilter.getKernel();
		_matrixTablePanel.setTableData(kernel.getKernelData(), kernel.getHeight(), kernel.getWidth());
	}

	@Override
	public void addPropertyChangeListener(PropertyChangeListener pcl) 
	{
		_pcsupport.addPropertyChangeListener(pcl);
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener pcl) 
	{
		_pcsupport.removePropertyChangeListener(pcl);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource().equals(_apply))
		{
			_erodeFilter.setKernel
					(
							new KernelJAI
							(
								_matrixTablePanel.getKernelHeight(),
								_matrixTablePanel.getKernelWidth(),
								_matrixTablePanel.translateKernel()
							)
					);
		}
	}

	/**
	 * Initialize the gui
	 */
	private void initGUI() 
	{
		_matrixTablePanel = new MatrixTablePanel();

		setLayout(new BorderLayout());

		_apply = new JButton("apply");
		_apply.addActionListener(this);

		JPanel leftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		leftPanel.add(new JLabel("erode matrix"));

		JPanel rightPanel = new JPanel(new BorderLayout());
		rightPanel.add(_matrixTablePanel, BorderLayout.CENTER);

		JPanel flowPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		flowPanel.add(_apply);
		rightPanel.add(flowPanel, BorderLayout.SOUTH);

		add(leftPanel, BorderLayout.WEST);
		add(rightPanel, BorderLayout.CENTER);
	}
}
